package com.stephen.jsbridge.utils;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.os.Message;
import android.provider.ContactsContract;
import android.text.TextUtils;

import com.stephen.jsbridge.jsbridge.BridgeHandler;
import com.stephen.jsbridge.jsbridge.CallBackFunction;

import org.json.JSONObject;

/**
 * Created by Administrator on 2016/8/5.
 */
public class DisposeWebViewJsCommand implements BridgeHandler {
    //Js请求Native命令
    public static final String Js_GoToChangeBank = "goToChangeBank";
    public static final String[] ExecActionHtml5AllCommandAry = new String[]{Js_GoToChangeBank};//MainHtml5WebViewActivity All Command
    //Native请求Js命令
    public static final String N_GoPreResultHandler = "goPreResultHandler";
    protected Activity baseActivity;
    protected JsCommandResultCallBack callbackContext;

    public DisposeWebViewJsCommand(Activity baseActivity) {
        this.baseActivity = baseActivity;
    }

    @Override
    public void handler(String data, CallBackFunction callBackFunction) {//必须传参,传参格式{"action":"--","param":"--","jsonData":"--"},至少得有action
        try{
            System.out.println("=====NativeDisposeJsBridgeCommand======>"+data);
            this.callbackContext = new JsCommandResultCallBack(callBackFunction);
            JSONObject jsonObject = null;
            String action = null,param = "";//操作命令和一个参数(默认空参)
            JSONObject jsonArgs = null;//两个或更多以上的参数
            if(!TextUtils.isEmpty(data) && (new JsonValidator()).validate(data)){
                jsonObject = new JSONObject(data);
                if(null != jsonObject && jsonObject.length() > 0){
                    if(jsonObject.has("action"))action = jsonObject.getString("action");
                    if(jsonObject.has("jsonArgs"))jsonArgs = jsonObject.getJSONObject("jsonArgs");
                    if(jsonObject.has("param")){
                        param = jsonObject.getString("param");
                        try{if(null == jsonArgs)jsonArgs = jsonObject.getJSONObject("param");}catch(Exception e){}
                    }//end of if
                }//end of if
            }//end of if
            if(TextUtils.isEmpty(action)){
                //System.out.println("=============>没有获取到交互命令action!");
                //callbackContext.error("没有获取到交互命令action!");
                return;
            }//end of if
            if(null == this.baseActivity){
                callbackContext.error("致命错误,没有上下文!");
                return;
            }//end of if
            if(null == jsonArgs)jsonArgs = new JSONObject();//empty init

            if(Js_GoToChangeBank.equals(action)){
                StephenToolUtils.showLongHintInfo(baseActivity,"Js调用到了原生方法,参数:"+param);
            }else{
                callbackContext.error("html5界面命令无匹配,忽略!");
            }
        }catch(Exception e){
            callbackContext.error("出现异常,异常信息:"+e.getMessage());
            e.printStackTrace();
        }
    }
}
